﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Services;
using System.Device.Location;
using System.Collections.ObjectModel;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Tasks;
using Windows.Devices.Geolocation;

namespace Mapping_Demo.Views
{
    public partial class IsMapOffline : PhoneApplicationPage
    {
        Geolocator geolocator;
        public IsMapOffline()
        {
            InitializeComponent();
        }

        private void DownloadOfflineMaps(object sender, EventArgs e)
        {
            MapDownloaderTask mdt = new MapDownloaderTask();
            mdt.Show();

            // To update already downloaded maps...
            //MapUpdaterTask mut = new MapUpdaterTask();
            //mut.Show();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            geolocator = new Geolocator();
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                InstructionalText.Text = "tap here to turn off connectivity";
                isConnected = true;
            }
            else
            {
                isConnected = false;
                InstructionalText.Text = "tap here to see if you have offline maps for a region";
            }

            try
            {
                geolocator.DesiredAccuracyInMeters = 50;
                // If the cached location is over 30 minutes old, get a new one
                TimeSpan maximumAge = new TimeSpan(0, 30, 0);
                // If we're trying for more than 45 seconds, abandon the operation
                TimeSpan timeout = new TimeSpan(0, 0, 5);
                Geoposition myLocation = await geolocator.GetGeopositionAsync(maximumAge, timeout);

                GeoCoordinate geoCord = new GeoCoordinate(myLocation.Coordinate.Latitude, myLocation.Coordinate.Longitude);
                myOfflineMap.Center = geoCord;
                myOfflineMap.ZoomLevel = 8;
            }
            catch { }
        }
        string instuctString = "";
        bool isConnected = true;
    
        private void TopBarTapped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (isConnected)
            {
                ConnectionSettingsTask cst = new ConnectionSettingsTask();
                cst.ConnectionSettingsType = ConnectionSettingsType.AirplaneMode;
                cst.Show();
            }
            else
            {
                myOfflineMap.SetView(myOfflineMap.Center, 13);
            }
        }

        private void GoToAirplaneSettings(object sender, EventArgs e)
        {
            ConnectionSettingsTask cst = new ConnectionSettingsTask();
            cst.ConnectionSettingsType = ConnectionSettingsType.AirplaneMode;
            cst.Show();
        }

    }
}